<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
	xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0">
	<xsl:param name="lang"/>
	<xsl:param name="sqlzen"/>
	<xsl:variable name="sqlzen-path" select='concat( "file:///", translate( translate( normalize-space( $sqlzen ), "&apos;", "" ), "\", "/" ) )'/>	
	<xsl:variable name="tablePrefix">
		<xsl:value-of select="/sqlzen/rule/affixes/dbObjects/table/@prefix"/>
	</xsl:variable>
	<xsl:variable name="tableSuffix">
		<xsl:value-of select="/sqlzen/rule/affixes/dbObjects/table/@suffix"/>
	</xsl:variable>
	<xsl:variable name="primaryKeyPrefix">
		<xsl:value-of select="/sqlzen/rule/affixes/dbObjects/primaryKey/@prefix"/>
	</xsl:variable>
	<xsl:variable name="primaryKeySuffix">
		<xsl:value-of select="/sqlzen/rule/affixes/dbObjects/primaryKey/@suffix"/>
	</xsl:variable>
	<xsl:variable name="foreignKeyPrefix">
		<xsl:value-of select="/sqlzen/rule/affixes/dbObjects/foreignKey/@prefix"/>
	</xsl:variable>
	<xsl:variable name="foreignKeySuffix">
		<xsl:value-of select="/sqlzen/rule/affixes/dbObjects/foreignKey/@suffix"/>
	</xsl:variable>
	<xsl:variable name="uniqueIndexPrefix">
		<xsl:value-of select="/sqlzen/rule/affixes/dbObjects/uniqueIndex/@prefix"/>
	</xsl:variable>
	<xsl:variable name="uniqueIndexSuffix">
		<xsl:value-of select="/sqlzen/rule/affixes/dbObjects/uniqueIndex/@suffix"/>
	</xsl:variable>
	<xsl:variable name="normalIndexPrefix">
		<xsl:value-of select="/sqlzen/rule/affixes/dbObjects/normalIndex/@prefix"/>
	</xsl:variable>
	<xsl:variable name="normalIndexSuffix">
		<xsl:value-of select="/sqlzen/rule/affixes/dbObjects/normalIndex/@suffix"/>
	</xsl:variable>
	<xsl:variable name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable>
	<xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
	<xsl:template name="putUCase">
		<xsl:param name="text"/>
		<xsl:value-of select="translate( $text, $lcletters, $ucletters )"/>
	</xsl:template>
	<xsl:template name="putLCase">
		<xsl:param name="text"/>
		<xsl:value-of select="translate( $text, $ucletters, $lcletters )"/>
	</xsl:template>
	<xsl:template name="putColumnType">
		<xsl:value-of select="translate( @type, $lcletters, $ucletters )"/>
		<xsl:call-template name="putTypeLength"/>
	</xsl:template>	
	<xsl:template name="putDbObjectName" xmlns="http://relaxng.org/ns/structure/1.0">
		<xsl:for-each
			select="wordRef | entityPrefixRef | entitySuffixRef | attributePrefixRef | attributeSuffixRef | tableNameRef | columnNameRef">
			<xsl:value-of select="@name"/>
		</xsl:for-each>
	</xsl:template>
	<xsl:template name="putCommentAnnotation">
		<xsl:for-each select="comment[ @lang = $lang ]">
			<xsl:variable name="text">
				<xsl:value-of select="normalize-space( text() )"/>
			</xsl:variable>
			<xsl:choose>
				<xsl:when test="@brief and string-length( $text ) > 0">
					<a:documentation>
						<xsl:value-of select="@brief"/> - <xsl:value-of select="$text"/>
					</a:documentation>
				</xsl:when>
				<xsl:when test="@brief">
					<a:documentation>
						<xsl:value-of select="@brief"/>
					</a:documentation>
				</xsl:when>
				<xsl:when test="string-length( $text ) > 0">
					<a:documentation>
						<xsl:value-of select="$text"/>
					</a:documentation>
				</xsl:when>
			</xsl:choose>
		</xsl:for-each>
	</xsl:template>
	<xsl:template name="putCommentText">		
			<xsl:variable name="text">
				<xsl:value-of select="normalize-space( text() )"/>
			</xsl:variable>
			<xsl:choose>
				<xsl:when test="@brief and string-length( $text ) > 0">
					<xsl:value-of select="@brief"/> - <xsl:value-of select="$text"/>
				</xsl:when>
				<xsl:when test="@brief">
					<xsl:value-of select="@brief"/>
				</xsl:when>
				<xsl:when test="string-length( $text ) > 0">
					<xsl:value-of select="$text"/>
				</xsl:when>
			</xsl:choose>		
	</xsl:template>
	<xsl:template name="putTypeLength">
		<xsl:choose>
			<xsl:when test="@length and @length2">(<xsl:value-of select="@length"/>,<xsl:value-of select="@length2"/>)</xsl:when>
			<xsl:when test="@length">(<xsl:value-of select="@length"/>)</xsl:when>
		</xsl:choose>
	</xsl:template>
	<xsl:template name="putPrimaryKeyName">
		<xsl:value-of select="$primaryKeyPrefix"/>
		<xsl:value-of select="concat( ../../@name, '_' )"/>
		<xsl:choose>
			<xsl:when test="count( primaryKey.column ) > 1">
				<xsl:value-of select="primaryKey.column[1]/@name"/>
				<xsl:text>_</xsl:text>
				<xsl:value-of select="primaryKey.column[2]/@name"/>
			</xsl:when>
			<xsl:when test="primaryKey.column">
				<xsl:value-of select="primaryKey.column/@name"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="putColumnName">
					<xsl:with-param name="name" select="@column"/>
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
		<xsl:value-of select="$primaryKeySuffix"/>
	</xsl:template>
	<xsl:template name="putUniqueIndexColumn">
		<xsl:choose>
			<xsl:when test="count( unique.column ) > 1">
				<xsl:for-each select="unique.column[1]">
					<xsl:call-template name="putColumnName">
						<xsl:with-param name="name" select="@name"/>
					</xsl:call-template>
					<xsl:if test="@length">(<xsl:value-of select="@length"/>)</xsl:if>
				</xsl:for-each>
				<xsl:text>, </xsl:text>
				<xsl:for-each select="unique.column[2]">
					<xsl:call-template name="putColumnName">
						<xsl:with-param name="name" select="@name"/>
					</xsl:call-template>
					<xsl:if test="@length">(<xsl:value-of select="@length"/>)</xsl:if>
				</xsl:for-each>
			</xsl:when>
			<xsl:when test="unique.column">
				<xsl:call-template name="putColumnName">
					<xsl:with-param name="name" select="unique.column/@name"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="putColumnName">
					<xsl:with-param name="name" select="@column"/>
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<xsl:template name="putUniqueIndexName">
		<xsl:value-of select="$uniqueIndexPrefix"/>
		<xsl:choose>
			<xsl:when test="count( unique.column ) > 1">
				<xsl:call-template name="putColumnName">
					<xsl:with-param name="name" select="unique.column[1]/@name"/>
				</xsl:call-template>
				<xsl:text>_</xsl:text>
				<xsl:call-template name="putColumnName">
					<xsl:with-param name="name" select="unique.column[2]/@name"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:when test="unique.column">
				<xsl:call-template name="putColumnName">
					<xsl:with-param name="name" select="unique.column/@name"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="putColumnName">
					<xsl:with-param name="name" select="@column"/>
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
		<xsl:value-of select="$uniqueIndexSuffix"/>
	</xsl:template>
	<xsl:template name="putColumnName">
		<xsl:param name="name"/>
		<xsl:variable name="tableName">
			<xsl:for-each select="ancestor::table/name">
				<xsl:call-template name="putDbObjectName"/>
			</xsl:for-each>
		</xsl:variable>
		<xsl:value-of select="substring-after( $name, concat( $tableName, '.' ) )"/>
	</xsl:template>
	<xsl:template name="putReferenceTableColumn">
		<xsl:param name="name"/>
		<xsl:variable name="tableName">
			<xsl:value-of select="substring-before( $name, '.')"/>
		</xsl:variable>
		<xsl:variable name="columnName">
			<xsl:value-of select="substring-after( $name, '.' )"/>
		</xsl:variable>
		<xsl:value-of select="concat( $tableName, '(', $columnName, ')' )"/>
	</xsl:template>	
	<xsl:template name="putNormalIndexColumn">
		<xsl:choose>
			<xsl:when test="count( index.column ) > 1">
				<xsl:for-each select="index.column[1]">
					<xsl:call-template name="putColumnName">
						<xsl:with-param name="name" select="@name"/>
					</xsl:call-template>
					<xsl:if test="@length">(<xsl:value-of select="@length"/>)</xsl:if>
				</xsl:for-each>
				<xsl:text>, </xsl:text>
				<xsl:for-each select="index.column[2]">
					<xsl:call-template name="putColumnName">
						<xsl:with-param name="name" select="@name"/>
					</xsl:call-template>
					<xsl:if test="@length">(<xsl:value-of select="@length"/>)</xsl:if>
				</xsl:for-each>
			</xsl:when>
			<xsl:when test="index.column">
				<xsl:call-template name="putColumnName">
					<xsl:with-param name="name" select="index.column/@name"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="putColumnName">
					<xsl:with-param name="name" select="@column"/>
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<xsl:template name="putNormalIndexName">
		<xsl:value-of select="$normalIndexPrefix"/>
		<xsl:choose>
			<xsl:when test="count( index.column ) > 1">
				<xsl:call-template name="putColumnName">
					<xsl:with-param name="name" select="index.column[1]/@name"/>
				</xsl:call-template>
				<xsl:text>_</xsl:text>
				<xsl:call-template name="putColumnName">
					<xsl:with-param name="name" select="index.column[2]/@name"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:when test="index.column">
				<xsl:call-template name="putColumnName">
					<xsl:with-param name="name" select="index.column/@name"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="putColumnName">
					<xsl:with-param name="name" select="@column"/>
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
		<xsl:value-of select="$normalIndexSuffix"/>
	</xsl:template>
	<xsl:template name="putForeignKeyName">
		<xsl:value-of select="$foreignKeyPrefix"/>
		<xsl:value-of select="concat( ../../@name, '_' )"/>
		<xsl:choose>
			<xsl:when test="count( foreignKey.column ) > 1">
				<xsl:call-template name="putColumnName">
					<xsl:with-param name="name" select="foreignKey.column[1]/@name"/>
				</xsl:call-template>
				<xsl:text>_</xsl:text>
				<xsl:call-template name="putColumnName">
					<xsl:with-param name="name" select="foreignKey.column[2]/@name"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:when test="foreignKey.column">
				<xsl:call-template name="putColumnName">
					<xsl:with-param name="name" select="foreignKey.column/@name"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="putColumnName">
					<xsl:with-param name="name" select="@column"/>
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
		<xsl:value-of select="$foreignKeySuffix"/>
	</xsl:template>
</xsl:stylesheet>
